from socketio import socketio_manage
from django.conf import settings

from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import user_passes_test
from django.shortcuts import get_object_or_404, render, redirect, render_to_response
from django.template import RequestContext

from chat.models import ChatRoom
from game.models import Game, Base

from django.core.urlresolvers import reverse

from django.conf import settings

def games(request, template="games.html"):
    """
    Homepage - lists all games.
    """
    context = {"games": Game.objects.all()}
    return render_to_response(template, context, RequestContext(request))

def prueba(request):
    template = "index.html"
    return render(request, template)

def prueba2(request):
    template = "prueba2.html"
    return render(request, template)

def game(request, id_game, template="game.html"):
    """
    Show a game.
    """
    game = get_object_or_404(Game, id=id_game)
    context = {
                "room": game.chat_room,
                "game": game,

                # SETTINGS VALUES
                'ATTACK_POWER': settings.ATTACK_POWER,
                'DEFENSE_POWER': settings.DEFENSE_POWER,
                'TRANSPORT_CAPACITY': settings.TRANSPORT_CAPACITY,
                'SPEED': settings.SPEED,
                'RESOURCES_COLLECTED': settings.RESOURCES_COLLECTED,
                'COST_RESOURCES': settings.COST_RESOURCES,
                'NAME_ANTS': settings.NAME_ANTS,
              }
    return render_to_response(template, context, context_instance=RequestContext(request))

def create(request):
    """
        Handles post from the "Add game" form on the homepage, and
        redirects to the new room.
    
        Map:
            (X, c, c, c)
            (c, c, O, c)
            (c, O, c, c)
            (c, c, c, X)
    
        X = 2 Players
        O = 2 Free Bases
    """
    name = request.POST.get("name")
    if name:
        game = Game(status='PY')
        game.save()
        try:
            new_bases = [
                Base(game=game, x_coordinate = 0, y_coordinate=0, resources=500),
                Base(game=game, x_coordinate = 1, y_coordinate=2, resources=500),
                Base(game=game, x_coordinate = 2, y_coordinate=1, resources=500),
                Base(game=game, x_coordinate = 3, y_coordinate=3, resources=500, bonus='P'),

                Base(game=game, x_coordinate = 0, y_coordinate=7, resources=500),
                Base(game=game, x_coordinate = 1, y_coordinate=5, resources=500),
                Base(game=game, x_coordinate = 2, y_coordinate=6, resources=500),
                Base(game=game, x_coordinate = 3, y_coordinate=4, resources=500, bonus='S'),

                Base(game=game, x_coordinate = 7, y_coordinate=0, resources=500),
                Base(game=game, x_coordinate = 5, y_coordinate=1, resources=500),
                Base(game=game, x_coordinate = 6, y_coordinate=2, resources=500),
                Base(game=game, x_coordinate = 4, y_coordinate=3, resources=500, bonus='D'),

                Base(game=game, x_coordinate = 7, y_coordinate=7, resources=500),
                Base(game=game, x_coordinate = 5, y_coordinate=6, resources=500),
                Base(game=game, x_coordinate = 6, y_coordinate=5, resources=500),
                Base(game=game, x_coordinate = 4, y_coordinate=4, resources=500, bonus='A')
            ]
            Base.objects.bulk_create(new_bases)

        except Exception as e:
            print e[0]
            return redirect(games)
        room = ChatRoom(name=name, game=game)
        room.save()
        return HttpResponseRedirect(reverse('game', args=[game.id]))
    return redirect(games)




